﻿package 
{
	import flash.display.Sprite;
	import flash.display.*;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.system.Capabilities;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.FontStyle;
	import flash.text.FontType;
	
	
	/**
	 * ...
	 * @author ...
	 */
	public class Main extends Sprite 
	{
		[Embed(source = 'Resources\\BetecknaLowerCase.ttf', fontName = '_Font')]
		private var m_dummy:String; // dummy var to embed font.

		private var m_playerControls:PlayerControls;
		private var m_slideshow:Slideshow;
		private var m_originalWidth:int;
		private var m_originalHeight:int;
		private var m_returnURL:String;

		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = StageQuality.HIGH;
			
			this.x = 0;
			this.y = 0;
			
			m_slideshow = new Slideshow();
			this.addChild(m_slideshow);
			
			if ( this.root.loaderInfo.parameters["slideTransitionTime"] != null )
			{
				m_slideshow.slideTransitionTime = Number(this.root.loaderInfo.parameters["slideTransitionTime"] as String);
			}
			
			if ( this.root.loaderInfo.parameters["returnURL"] != null )
			{
				m_returnURL = String(this.root.loaderInfo.parameters["returnURL"]);
			}
				
			loadSlides();

			addPlayerControls();
			// Add a listener to allow the slideshow to resize as necessary.
			stage.addEventListener( Event.RESIZE, resizeHandler );
		}
		
		private function loadSlides():void
		{
			var paramList:Object = this.root.loaderInfo.parameters;
			var images:Array = null;
			var descriptions:Array = null;
			if ( paramList["imageList"] != null && paramList["descriptionList"] != null )
			{
				images = (paramList["imageList"] as String).split("||");
				descriptions = (paramList["descriptionList"] as String).split("||");
			}
			else
			{
				images = "http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1133.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1171.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1177.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1183.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1187.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1188.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1192.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1214.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1224.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1252.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1255.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1265.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1271.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1277.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1314.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1332.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1333.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1375.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1390.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1392.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1408.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1416.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1421.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1422.JPG.html?p=*full-image||http://www.markandaudrey.co.uk/photos/2008/London%20Sights/DSC_1439.JPG.html?p=*full-image".split("||");
				descriptions = "Southwark Cathedral||Southwark Cathedral||Blackfriars Station||Blackfriars Station||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||St. Dunstan's in the East||The Bank of England||The Bank of England||The Bank of England||Museum of London||St. Paul's Cathedral||St. Paul's Cathedral||Tate Modern||Millenium Bridge &amp; Tate Modern||Millenium Bridge &amp; Tate Modern||Millenium Bridge &amp; Tate Modern||Wandle Park".split("||");
			}
			for ( var x:int = 0; x < images.length; x++ )
			{
				trace( "Adding image " + images[x] );
				m_slideshow.addSlide( images[x] as String, descriptions[x] as String );
			}
			//m_slideshow.gotoSlide(0);
		}
		
		private function addPlayerControls():void
		{
			var slideTime:int;
			var showCaptionsDefault:Boolean = false;
			if ( this.root.loaderInfo.parameters["slideTime"] != null )
			{
				slideTime = int( this.root.loaderInfo.parameters["slideTime"] as String );
			}
			if ( this.root.loaderInfo.parameters["showCaptionsDefault"] != null )
			{
				showCaptionsDefault = Boolean( (this.root.loaderInfo.parameters["showCaptionsDefault"] as String)=="true" )
			}
			var slideStartInd:int;
			slideStartInd = 0;
			if ( this.root.loaderInfo.parameters["slideStartInd"] != null )
			{
				slideStartInd = int( this.root.loaderInfo.parameters["slideStartInd"] as String );
			}
			m_playerControls = new PlayerControls(m_slideshow, true, slideTime, showCaptionsDefault, slideStartInd);

			// Bind control events
			m_playerControls.addEventListener( PlayerControls.FULLSCREEN, fullScreenClickHandler );
			m_playerControls.addEventListener( PlayerControls.EXIT, exitClickHandler );
			
			// Position bottom-left
			m_playerControls.x = stage.stageWidth - m_playerControls.width;
			m_playerControls.y = stage.stageHeight - m_playerControls.height;
			this.addChild( m_playerControls );

			// Add the listener to ensure that the player controls are always on top.
			this.addEventListener( Event.ADDED, alwaysOnTop );
		}

		private function resizeHandler(event:Event):void
		{
			trace("Resizing...");
			if ( m_slideshow != null )
			{
				m_slideshow.resize();
			}
			m_playerControls.x = stage.stageWidth - m_playerControls.width;
			m_playerControls.y = stage.stageHeight - m_playerControls.height;
		}
		
		private function alwaysOnTop(event:Event):void
		{
			if ( this.numChildren - 1 > this.getChildIndex( m_playerControls ) )
			{
				this.setChildIndex( m_playerControls, this.numChildren - 1 );
			}
		}

		private function fullScreenClickHandler(event:Event):void
		{
			if ( stage.displayState == StageDisplayState.FULL_SCREEN )
			{
				trace( "Exit full screen click" );
				stage.displayState = StageDisplayState.NORMAL;
				stage.stageWidth = m_originalWidth;
				stage.stageHeight = m_originalHeight;
			}
			else
			{
				trace( "Enter full screen click" );
				m_originalWidth = stage.stageWidth;
				m_originalHeight = stage.stageHeight;
				stage.displayState = StageDisplayState.FULL_SCREEN;
				stage.stageWidth = Capabilities.screenResolutionX;
				stage.stageHeight = Capabilities.screenResolutionY;
			}
		}

		private function exitClickHandler(event:Event):void
		{
			trace( "Exit slideshow click" );
			// Exit full screen if we're in full screen.
			if ( stage.displayState == StageDisplayState.FULL_SCREEN )
			{
				stage.displayState = StageDisplayState.NORMAL;
				stage.stageWidth = m_originalWidth;
				stage.stageHeight = m_originalHeight;
			}
			var URLReq:URLRequest = new URLRequest(m_returnURL);
			try
			{
				navigateToURL(URLReq, "_self");
			}
			catch (e:Error)
			{
				trace( "Error exiting: " + e );
			}
		}
	}
}